package com.webside.interfaces.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.webside.distinguish.service.SeriesDistinguishService;
import com.webside.enums.ValidateMsg;
import com.webside.interfaces.model.SeriesModel;
import com.webside.interfaces.model.SeriesVideoTree;
import com.webside.interfaces.service.AccessService;
import com.webside.play.service.PlayService;
import com.webside.series.service.SeriesService;
import com.webside.util.JsonResult;

@RestController
@RequestMapping("/access")
public class AccessController {
	@Autowired
	PlayService playService;
	@Autowired
	SeriesDistinguishService seriesDistinguishService;
	@Autowired
	AccessService accessService;
	
	@RequestMapping(value="/get/seriesList", method=RequestMethod.POST)
	public JsonResult getSeriesList(HttpServletRequest request){
		String appid = request.getParameter("appid");
		String sign = request.getParameter("sign");
		Long timestamp = request.getParameter("timestamp")==null?null:Long.parseLong(request.getParameter("timestamp"));
		Integer pageNo = request.getParameter("pageNo")==null?1:Integer.parseInt(request.getParameter("pageNo"));
		String channel = request.getParameter("channel");
		//非空检验
		if (appid == null) {
			return JsonResult.failure("appid不能为空");
		}
		if (sign == null) {
			return JsonResult.failure("签名不能为空");
		}
		if (timestamp == null) {
			return JsonResult.failure("时间戳不能为空");
		}
		
		/*System.out.println("appid--"+appid);
		System.out.println("sign--"+sign);
		System.out.println("timestamp--"+timestamp);
		System.out.println("pageNo--"+pageNo);
		System.out.println("channel--"+channel);*/
		String message = playService.validateNoTime(appid, timestamp, sign);
		//没有成功通过
		if (!message.equals(ValidateMsg.ACCESS.getMsg())) {
			return JsonResult.failure(message);
		}
		//通过校验
		//根据站点id获取视频列表
		List<SeriesModel> list = seriesDistinguishService.selectSeriesListByAppid(appid, channel, pageNo);
		/*System.out.println(list.size());
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}*/
		int count = seriesDistinguishService.selectCountByAppid(appid, channel, pageNo);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("totalPage", count);
		map.put("size", 50);
		
		return JsonResult.success("success",map);
	}
	
	@RequestMapping(value="/get/seriesDetail", method=RequestMethod.POST)
	public JsonResult getSeriesDetail(HttpServletRequest request){
		String appid = request.getParameter("appid");
		String sign = request.getParameter("sign");
		Long timestamp = request.getParameter("timestamp")==null?null:Long.parseLong(request.getParameter("timestamp"));
		Integer seriesId = request.getParameter("seriesId")==null?null:Integer.parseInt(request.getParameter("seriesId"));
		//非空检验
		if (appid == null) {
			return JsonResult.failure("appid不能为空");
		}
		if (sign == null) {
			return JsonResult.failure("签名不能为空");
		}
		if (timestamp == null) {
			return JsonResult.failure("时间戳不能为空");
		}
		if (seriesId == null) {
			return JsonResult.failure("剧集id不能为空");
		}
		String message = playService.validateNoTime(appid, timestamp, sign);
		//没有成功通过
		if (!message.equals(ValidateMsg.ACCESS.getMsg())) {
			return JsonResult.failure(message);
		}
		//校验该seriesId是否存在于该站点对应分组中
		String message1 = accessService.validateSeries(seriesId, appid);
		if (!message1.equals(ValidateMsg.ACCESS.getMsg())) {
			return JsonResult.failure(message);
		}
		//根据seriesId获取详细信息和videos
		SeriesVideoTree list = seriesDistinguishService.selectBySeriesId(seriesId);
		
		return JsonResult.success(list);
	}
	
	@RequestMapping(value="/get/offlineList", method=RequestMethod.POST)
	public JsonResult getOfflineList(HttpServletRequest request){
		String appid = request.getParameter("appid");
		String sign = request.getParameter("sign");
		Long timestamp = request.getParameter("timestamp")==null?null:Long.parseLong(request.getParameter("timestamp"));
		Integer pageNo = request.getParameter("pageNo")==null?1:Integer.parseInt(request.getParameter("pageNo"));
		String channel = request.getParameter("channel");
		//非空检验
		if (appid == null) {
			return JsonResult.failure("appid不能为空");
		}
		if (sign == null) {
			return JsonResult.failure("签名不能为空");
		}
		if (timestamp == null) {
			return JsonResult.failure("时间戳不能为空");
		}
		
		/*System.out.println("appid--"+appid);
		System.out.println("sign--"+sign);
		System.out.println("timestamp--"+timestamp);
		System.out.println("pageNo--"+pageNo);
		System.out.println("channel--"+channel);*/
		String message = playService.validateNoTime(appid, timestamp, sign);
		//没有成功通过
		if (!message.equals(ValidateMsg.ACCESS.getMsg())) {
			return JsonResult.failure(message);
		}
		//通过校验
		//根据站点id获取视频列表
		List<SeriesModel> list = seriesDistinguishService.selectOfflineSeriesListByAppid(appid, channel, pageNo);
		/*System.out.println(list.size());
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}*/
		int count = seriesDistinguishService.selectOfflineCountByAppid(appid, channel, pageNo);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("totalPage", count);
		map.put("size", 50);
		
		return JsonResult.success("success",map);
	}
	
}
